package cn.ffcs.search.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.ffcs.base.web.PageControl;
import cn.ffcs.organ.model.LinkMethod;
import cn.ffcs.product.model.ProductManager;
import cn.ffcs.product.service.IProductManagerService;
import cn.ffcs.product.service.ProductService;
import cn.ffcs.search.model.Condition;
import cn.ffcs.search.model.Search;
import cn.ffcs.search.service.ISearchService;
import cn.ffcs.util.MyList;
import cn.ffcs.search.model.Condition;
/**
 * 产品目录实体类：定义基本属性<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">g.huangjx</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:2009-2-26</td>
 * <td>修改人:g.huanjx</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author g.huangjx
 * @version $Id$
 */
public class SearchAction  extends ActionSupport
{
    private ISearchService searchService;
    /** 统一搜索 **/
    private Search search;
    /** 统一搜索结果集**/
    private List<Search> searchs;
    /** 分页信息**/
    private int[] str={1,10,1};//面页总数，一页数，当前页
    /** 查询条件**/
    private Condition conditon;
    /** 设置分页 */
    private PageControl pageControl;

    private HashMap<String, Object> conds;
    
    /** 产品经理集 **/
    private List productManagers; 

    /** 专家搜索标示 **/
    private String seachFlag;

    /** 产品服务 **/
    private ProductService productService;
    
    
    
    public String findByPage(){
        ActionContext ctx = ActionContext.getContext();   
        HttpServletResponse response=(HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
        if(conditon!=null){
            try{
                MyList.toChina(conditon, "utf-8");
                if(conditon!=null&&conditon.getContent()!=null&&!"".equals(conditon.getContent().trim())){
                    searchs= searchService.findAllSearchByAnnex(conditon,str);
                }else{
                    searchs= searchService.findAllSearch(conditon,str);
                }
                String xml=MyList.getAll(searchs,str);
                response.setCharacterEncoding("gb2312");//
                response.setContentType("text/xml");//
                response.getWriter().write(xml);
            }catch(Exception e){
                e.printStackTrace();
            }
            return null;
        }else{
            return SUCCESS;
        }
        
    }
    public String toNetMap(){
        return SUCCESS;
    }
    
    /**
     * 专家网络 
     * TODO 请输入方法说明
     */
    public String toExpNet()
    {
        this.findProductManagerView();
        // 如果第一次装置，默认参数
        if (pageControl == null)
        {
            pageControl = new PageControl(productManagers.size(), 1, 10, 0);
        }
        else
        {
            pageControl = new PageControl(productManagers.size(), pageControl
                    .getPageNo(), 10, 0);
        }
        productManagers = this.offset(productManagers);
        pageControl.setResults(productManagers);
        return SUCCESS;
    }
    
    public List offset(List list){
        int offset=0;
        int subNo=0;
        str[0]=list.size();
        int pageNo=pageControl.getPageNo();
        int pageSize=pageControl.getPageSize();
        
        if(list.size()>=pageNo*pageSize){
            offset=(pageNo-1)*pageSize+1;
        }else{
            offset=list.size()/pageSize*pageSize;
        }
        if(list.size()>pageNo*pageSize){
            subNo=offset+pageSize;
        }else{
            subNo=list.size();
        }
        return list.subList(offset,subNo);
    }
    
    /**
     * 获取数据集，根据用户、主要负责人排序，用户名称模糊查询
     * TODO 请输入方法说明
     * @return
     */
    public void findProductManagerView()
    {
        if (conds == null)
        {
            conds=new HashMap<String, Object>();
            conds.put("name", "");
        }
        List<ProductManager> list = searchService.findProductManagerView(conds
                .get("name")+ "");
        ProductManager temp;
        ProductManager tmp;
        productManagers = new ArrayList();
        while (list != null && list.size() > 0)
        {
            temp = list.get(0);
            if (list.size() > 1)
            {
                tmp = list.get(1);
                if (temp.getPerson().equals(tmp.getPerson()))
                {
                    tmp.getProduct().setName(
                            (temp.getProduct().getName() + "  " + tmp
                                    .getProduct().getName()));
                }
                else
                {
                    add(productManagers, temp);
                }
                list.remove(temp);
            }
            else
            {
                add(productManagers, temp);
                break;
            }
        }
        return;
    }
    
    public void add(List list,ProductManager temp){
        Search search=new Search();
        search.setCreator(temp.getPerson().getName());
        search.setTitle(temp.getProduct().getName());
        search.setBegin(temp.getBegin());
        search.setModuleid(new Integer(temp.getIsPrimary()));
        List<LinkMethod> li=temp.getPerson().getLinkMethods();
        String linkMethod="";
        LinkMethod lMethod;
        List list1;
        if(li!=null){
            list1=new ArrayList();
            for(int i=0;i<li.size();i++){
                lMethod=li.get(i);
                if(lMethod!=null){
                    list1.add(lMethod);
                    linkMethod+=lMethod.getType()+":"+lMethod.getValue()+" ";
                }
            }
            search.setList(list1);
        }
        search.setModule(linkMethod);
        list.add(search);
    }
    
    public ISearchService getSearchService()
    {
        return searchService;
    }

    public void setSearchService(ISearchService searchService)
    {
        this.searchService = searchService;
    }

    public Search getSearch()
    {
        return search;
    }

    public void setSearch(Search search)
    {
        this.search = search;
    }

    public List<Search> getSearchs()
    {
        return searchs;
    }

    public void setSearchs(List<Search> searchs)
    {
        this.searchs = searchs;
    }
    public int[] getStr()
    {
        return str;
    }
    public void setStr(int[] str)
    {
        this.str = str;
    }
    public Condition getConditon()
    {
        return conditon;
    }
    public void setConditon(Condition conditon)
    {
        this.conditon = conditon;
    }
    public HashMap<String, Object> getConds()
    {
        return conds;
    }
    public void setConds(HashMap<String, Object> conds)
    {
        this.conds = conds;
    }
    public List getProductManagers()
    {
        return productManagers;
    }
    public void setProductManagers(List productManagers)
    {
        this.productManagers = productManagers;
    }
    public PageControl getPageControl()
    {
        return pageControl;
    }
    public void setPageControl(PageControl pageControl)
    {
        this.pageControl = pageControl;
    }
    public String getSeachFlag()
    {
        return seachFlag;
    }
    public void setSeachFlag(String seachFlag)
    {
        this.seachFlag = seachFlag;
    }
    public ProductService getProductService()
    {
        return productService;
    }
    public void setProductService(ProductService productService)
    {
        this.productService = productService;
    }
}
